<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// 日志管理
class Logger {
   
    const LOG_ACTION_TYPE_ADD = 1;    // 新增操作
    const LOG_ACTION_TYPE_UPDATE = 2; // 修改操作
    const LOG_ACTION_TYPE_DELETE = 3; // 删除操作
    const LOG_ACTION_TYPE_SYNC = 4;   // 同步操作
    const LOG_ACTION_TYPE_IMPORT = 5; // 导入操作
    const LOG_ACTION_TYPE_BRAND = 6;  // 班牌管理操作

    public function __construct(){
        $this->CI = &get_instance();
 
        $this->CI->load->model('schoolModel');
        $this->CI->load->model('accountModel');
    }

    // 保存日志
    public function save_log_action($dataArr) {
        if(!isset($dataArr['school_id'])) {
            throw new Exception('日志参数错误');
        }
        if(!isset($dataArr['campus_id'])) {
            throw new Exception('日志参数错误');
        }
        if(!isset($dataArr['user_type'])) {
            throw new Exception('日志参数错误');
        }
        if(!isset($dataArr['user_id'])) {
            throw new Exception('日志参数错误');
        }
        if(!isset($dataArr['realname'])) {
            throw new Exception('日志参数错误');
        }
        if(!isset($dataArr['action_type'])) {
            throw new Exception('日志参数错误');
        }
        if(!isset($dataArr['content'])) {
            throw new Exception('日志参数错误');
        }
        $dataArr['createtime'] = date('Y-m-d H:i:s');
        // 获取远程id
        $ip = $this->get_remote_ip();
        $dataArr['ip'] = $ip;

        if($this->CI->accountModel->save_log_action($dataArr) == false) {
            throw new Exception('日志参数错误');
        }
    }

    // 获取操作类型数值
    public function get_action_type($name) {
        switch($name) {
            case 'add':
                return self::LOG_ACTION_TYPE_ADD;
                break;
            case 'update':
                return self::LOG_ACTION_TYPE_UPDATE;
                break;
            case 'delete':
                return self::LOG_ACTION_TYPE_DELETE;
                break;
            case 'sync':
                return self::LOG_ACTION_TYPE_SYNC;
                break;
            case 'import':
                return self::LOG_ACTION_TYPE_IMPORT;
                break;
            case 'brand':
                return self::LOG_ACTION_TYPE_BRAND;
                break;
            return false;
        }
    }

    // 获取操作类型列表
    public function get_action_type_list() {
        return array(
            array(
                'title' => '新增',
                'value' => self::LOG_ACTION_TYPE_ADD
            ),
            array(
                'title' => '修改',
                'value' => self::LOG_ACTION_TYPE_UPDATE
            ),
            array(
                'title' => '删除',
                'value' => self::LOG_ACTION_TYPE_DELETE
            ),
            array(
                'title' => '同步',
                'value' => self::LOG_ACTION_TYPE_SYNC
            ),
            array(
                'title' => '导入',
                'value' => self::LOG_ACTION_TYPE_IMPORT
            ),
            array(
                'title' => '班牌管理',
                'value' => self::LOG_ACTION_TYPE_BRAND
            )
        );
    }

    // 获取操作类型中文名称
	public function get_action_type_chinese($actionType) {
        switch($actionType) {
            case self::LOG_ACTION_TYPE_ADD:
                return '<span class="text-success">新增</span>';
                break;
            case self::LOG_ACTION_TYPE_UPDATE:
                return '<span class="text-info">修改</span>';
                break;
            case self::LOG_ACTION_TYPE_DELETE:
                return '<span class="text-danger">删除</span>';
                break;
            case self::LOG_ACTION_TYPE_SYNC:
                return '<span class="text-warning">同步</span>';
                break;
            case self::LOG_ACTION_TYPE_IMPORT:
                return '<span class="text-warning">导入</span>';
                break;
            case self::LOG_ACTION_TYPE_BRAND:
                return '<span class="text-warning">班牌管理</span>';
                break;
            return '';
        }
    }

    // 获取用户类型中文名称
	public function get_user_type_chinese($userType) {
        switch($userType) {
            case USER_TYPE_SUPER_ADMIN:
                return '<span class="text-success">超管</span>';
                break;
            case USER_TYPE_SCHOOL:
                return '<span class="text-info">学校管理员</span>';
                break;
            case USER_TYPE_TEACHER:
                return '<span class="text-danger">教师</span>';
                break;
            return '';
        }
    }

    // 获取ip地址
    public function get_remote_ip() {
        $ip = '';
        if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } elseif(isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } else{
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        $ipArr = explode(',', $ip);
        return $ipArr[0];
    }
}